package com.lzy.crm.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.Dict;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.DictService;
import com.lzy.crm.service.ParamService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.util.UserConstants;
import com.lzy.crm.vo.SystemStatusInfo;

/**
 *  设置
 * @author csp
 *
 */
@Controller
@RequestMapping("/admin")
public class SystemController {
	
	
	Logger logger= Logger.getLogger(SystemController.class);
	
	
	@Resource
	ParamService paramService;
	
	@Resource
	DictService dictService;
	
	@RequestMapping("/system_mobile")  
	public String system(HttpServletRequest request,ModelMap model) {
		
		return "mobile/system/system_main";
	}
	
	
	
	
	
	/**
	 * 微信首页游戏列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/param")  
	public String notice(HttpServletRequest request,ModelMap model) {
		String statusName =  "RoomCardMenInvited" ;//request.getParameter("statusName");
		List<SystemStatusInfo> systemStatusInfoList=paramService.getSystemStatusInfoByStatusName(statusName);
		model.put("systemStatusInfoList", systemStatusInfoList);
		return "param/param";
	}
	
	/**
	 * 个人信息保存
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/param_edit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String doEditUser(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
				bootJsonResponse.setMessage(" 权限不够，请联系管理员 ");
				return JsonUtil.toJson(bootJsonResponse); 
			}
			String statusName=request.getParameter("statusName");
			Integer statusValue =Integer.parseInt(request.getParameter("statusValue"));
			SystemStatusInfo systemStatusInfo = new SystemStatusInfo();
			systemStatusInfo.setStatusName(statusName);
			systemStatusInfo.setStatusValue(statusValue);
			paramService.updateSystemStatusInfo(systemStatusInfo);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	@RequestMapping("/param_notile_mobile")  
	public String paramnoticeMobile(HttpServletRequest request,ModelMap model) {
		// + 提示消息
		List<SystemStatusInfo> shopTxt=paramService.getSystemStatusInfoByStatusName("NC_SHOP_TXT");
		//滚动消息
		List<SystemStatusInfo> scNotice =paramService.getSystemStatusInfoByStatusName("HN_SC_NOTICE");
		//
		//List<SystemStatusInfo> homeNotice=paramService.getSystemStatusInfoByStatusName("Home_Msg_NOTICE");
		
		
		// 房间邀请赠送
		List<SystemStatusInfo> roomInvitedList=paramService.getSystemStatusInfoByStatusName("RoomCardMenInvited");
		
		//是否放开代理后台充房卡
		List<Dict> dictList = dictService.getDictListByDictType("addCard");
		
		if(dictList!=null&&dictList.size()>0){
			model.put("dict", dictList.get(0));
		}
		if(roomInvitedList!=null&&roomInvitedList.size()>0){
			model.put("roomInvited", roomInvitedList.get(0));
		}
		if(shopTxt!=null&&shopTxt.size()>0){
			model.put("shopTxt", shopTxt.get(0));
		}
		if(scNotice!=null&&scNotice.size()>0){
			model.put("scNotice", scNotice.get(0));
		}
		//model.put("homeNotice", homeNotice.get(0));
		return "mobile/param/param_notice";
	}
	
	@RequestMapping(path="/param_notile_mobile_edit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String param_notile_mobile_edit(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
				bootJsonResponse.setMessage(" 权限不够，请联系管理员 ");
				return JsonUtil.toJson(bootJsonResponse); 
			}
			String statusName=request.getParameter("statusName");
			String statusString = request.getParameter("statusString");
			SystemStatusInfo systemStatusInfo = new SystemStatusInfo();
			systemStatusInfo.setStatusName(statusName);
			systemStatusInfo.setStatusString(statusString);;
			paramService.updateSystemStatusInfo(systemStatusInfo);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}

}
